<?php

namespace backend\controllers;

use backend\models\Stock;
use Yii;
use backend\models\StockOut;
use backend\models\StockOutSearch;
use yii\web\Controller;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * StockOutController implements the CRUD actions for StockOut model.
 */
class StockOutController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }
    public function beforeAction($action)
    {
        //如果未登录，则直接跳转
        if(Yii::$app->user->isGuest){
            return $this->redirect(['/site/login']);
        }

        $action = Yii::$app->controller->action->id;
        $controller = Yii::$app->controller->id;
        if(\Yii::$app->user->can('stock')){
           return true;
        }else{
            throw new ForbiddenHttpException(Yii::t('app','Sorry , You don\'t have permission'));
        }
    }
    /**
     * Lists all StockOut models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new StockOutSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single StockOut model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new StockOut model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate($id = null)
    {
        if(!\Yii::$app->user->can('stock-out-edit')){
            throw new ForbiddenHttpException(Yii::t('app','Sorry , You don\'t have permission'));
        }
        $stock = $id == null ? new Stock() : Stock::findOne($id);
        $stockOut = new StockOut();
        if ($stock->load(Yii::$app->request->post()) && $stockOut->load(Yii::$app->request->post())) {
            if($stock->validate() && $stockOut->validate()){
                if($stockOut->addStockOut($stock, $stockOut)){
                    return $this->redirect(['view', 'id' => $stockOut->id]);
                }else{
                    echo "<script>alert('出库失败：{$stockOut->_error}')</script>";
                    return $this->render('create', [
                        'stock' => $stock,
                        'stockOut' => $stockOut
                    ]);
                }
            }
            return $this->redirect(['view', 'id' => $stockOut->id]);
        } else {
            return $this->render('create', [
                'stock' => $stock,
                'stockOut' => $stockOut
            ]);
        }
    }

    /**
     * Updates an existing StockOut model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        if(!\Yii::$app->user->can('stock-out-edit')){
            throw new ForbiddenHttpException(Yii::t('app','Sorry , You don\'t have permission'));
        }
        $stockOut = $this->findModel($id);
        $stockId = (int)$stockOut->stock_id;
        $stock = Stock::findModel($stockId);
        if($stock == null){
            throw new NotFoundHttpException('The data does not exist.');
        }
        if ($stock->load(Yii::$app->request->post()) && $stockOut->load(Yii::$app->request->post())) {
            $stockOut->updateStockOut($stock, $stockOut);
            return $this->redirect(['view', 'id' => $stockOut->id]);
        } else {
            return $this->render('update', [
                'stock' => $stock,
                'StockOut' => $stockOut
            ]);
        }
    }
    /**
     * Deletes an existing StockOut model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        if(!\Yii::$app->user->can('stock-out-edit')){
            throw new ForbiddenHttpException(Yii::t('app','Sorry , You don\'t have permission'));
        }
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the StockOut model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return StockOut the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = StockOut::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
